package com.btb.adapter.contract;

import java.math.BigInteger;
import java.util.List;
import java.util.Objects;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.web3j.abi.datatypes.Type;
import org.web3j.abi.datatypes.Utf8String;
import org.web3j.abi.datatypes.generated.Uint256;
import org.web3j.protocol.core.DefaultBlockParameter;
import org.web3j.protocol.core.methods.response.TransactionReceipt;

import com.btb.adapter.utils.Fmt;
import com.btb.adapter.vo.FishDetail;
import com.btb.adapter.vo.OffChainEventEventResponse;
import com.btb.adapter.vo.UpChainEventEventResponse;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service
public class ActFishService {

	@Autowired
	private ActFish actFish;

	public TransactionReceipt upChain(String _address, BigInteger _tokenId, BigInteger _season, BigInteger _race,
			BigInteger _productionDate, BigInteger _showType, BigInteger _transactionNum, String _cardTips,
			String _cardImage, String _parts) {
		try {
			return actFish.upChain(_address, _tokenId, _season, _race, _productionDate, _showType, _transactionNum,
					_cardTips, _cardImage, _parts).send();
		} catch (Exception e) {
			log.error("ActFishService upChain err:{}", e.getMessage(), e);
		}
		return null;
	}

	public TransactionReceipt offChain(String _address, List<BigInteger> _tokenIds, BigInteger _clientid) {
		try {
			return actFish.offChain(_address, _tokenIds, _clientid).send();
		} catch (Exception e) {
			log.error("ActFishService offChain err:{}", e.getMessage(), e);
		}
		return null;
	}

	public FishDetail fishDetail(BigInteger _tokenId) {
		try {
			List<Type> fishDetail = actFish.fishDetail(_tokenId);
			if(Objects.isNull(fishDetail)) {
				return null;
			}
			Uint256 season = (Uint256)fishDetail.get(0);
			Uint256 race = (Uint256)fishDetail.get(1);
			Uint256 productionDate = (Uint256)fishDetail.get(2);
			Uint256 showType = (Uint256)fishDetail.get(3);
			Uint256 transactionNum = (Uint256)fishDetail.get(4);
			Utf8String cardTips = (Utf8String)fishDetail.get(5);
			Utf8String cardImage = (Utf8String)fishDetail.get(6);
			Utf8String parts = (Utf8String)fishDetail.get(7);
			// 如果返回的都是默认值 ,  说明没有
			if(Objects.equals(season, new Uint256(new BigInteger("0")))
					&& Objects.equals(race, new Uint256(new BigInteger("0")))
					&& Objects.equals(productionDate, new Uint256(new BigInteger("0")))
					&& Objects.equals(showType, new Uint256(new BigInteger("0")))
					&& Objects.equals(transactionNum, new Uint256(new BigInteger("0")))
					&& Objects.equals(cardTips, new Utf8String(StringUtils.EMPTY))
					&& Objects.equals(cardImage, new Utf8String(StringUtils.EMPTY))
					&& Objects.equals(parts, new Utf8String(StringUtils.EMPTY))
					) {
				return null;
			}
			return FishDetail.builder()
					.tokenId(_tokenId.longValue())
					.season(Fmt.btoi(season))
					.race(Fmt.btoi(race))
					.productionDate(Fmt.btol(productionDate))
					.showType(Fmt.btoi(showType))
					.transactionNum(Fmt.btoi(transactionNum))
					.cardTips(Fmt.utf8tos(cardTips))
					.cardImage(Fmt.utf8tos(cardImage))
					.parts(Fmt.utf8tos(parts))
					.build();
		} catch (Exception e) {
			log.error("ActFishService fishDetail err:{}", e.getMessage(), e);
		}
		return null;
	}

	public List<UpChainEventEventResponse> upChainEvent(String startBlockNumber, String endBlockNumber) {
		return  actFish.upChainEventEventFlowableEx(DefaultBlockParameter.valueOf(new BigInteger(startBlockNumber)),
				DefaultBlockParameter.valueOf(new BigInteger(endBlockNumber)));
	}

	public List<OffChainEventEventResponse> offChainEvent(String startBlockNumber, String endBlockNumber) {
		return actFish.offChainEventEventFlowableEx(DefaultBlockParameter.valueOf(new BigInteger(startBlockNumber)),
				DefaultBlockParameter.valueOf(new BigInteger(endBlockNumber)));
	}

}
